package cn.com.nes.site.entity.vo.project;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.id.IdUtils;
import cn.com.nes.site.entity.bo.DeviceSaveBo;
import cn.com.nes.site.entity.bo.callcenter.CallCenterEquipmentArchivesBo;
import cn.com.nes.site.entity.bo.ctce.CtceEquipmentArchivesBo;
import cn.com.nes.site.entity.em.BusinessType;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 添加个人项目模型（清洁取暖）
 */
@Data
public class PersonalProjectSaveForCleanVo extends PersonalProjectSaveVo {

    /**
     * 地点ID
     */
    private String locationId;

    /**
     * 安装协议ID
     */
    private String installProtocolId;

    @Valid
    @NotNull(message = "用户信息")
    private CustomerSaveVo customerSaveVo;

    @Valid
    @NotNull(message = "安装信息")
    private InstallSaveVo installSaveVo;

    @Valid
    @NotNull(message = "建筑信息")
    private BuildingSaveVo buildingSaveVo;

    @Valid
    @NotEmpty(message = "设备信息")
    private List<DeviceSaveVo> deviceSaveVos;

    private List<DeviceSaveBo> deviceSaveBos;

    @Valid
    @NotNull(message = "图纸信息")
    private ImageSaveVo imageSaveVo;


    /**
     * 转换为清洁取暖设备档案数据模型
     * @return
     */
    public CtceEquipmentArchivesBo toCtceEquipmentArchivesBo() throws Exception {
        CtceEquipmentArchivesBo bo = new CtceEquipmentArchivesBo();
        bo.setLocationId(this.getLocationId());
        bo.setInstallProtocolId(this.getInstallProtocolId());
        // 用户信息
        bo.setIdcard(this.getCustomerSaveVo().getIdCardNo().toUpperCase());
        bo.setMainname(this.getCustomerSaveVo().getMasterName());
        bo.setPhone(this.getCustomerSaveVo().getPhone());
        bo.setAreacode(this.getCustomerSaveVo().getAreaCode());
        bo.setJd(this.getCustomerSaveVo().getJd());
        bo.setWd(this.getCustomerSaveVo().getWd());
        bo.setAddress(this.getCustomerSaveVo().getAddress());
        bo.setMph(this.getCustomerSaveVo().getMph());
        bo.setDbh(this.getCustomerSaveVo().getDbh());
        bo.setKhbh(this.getCustomerSaveVo().getKhbh());
        bo.setYhztxz(this.getCustomerSaveVo().getYhztxz());
        // 安装信息
        bo.setAzry(this.getInstallSaveVo().getAzry());
        bo.setAzrydh(this.getInstallSaveVo().getAzrydh());
        bo.setAzzt(this.getInstallSaveVo().getAzzt());
        bo.setAzsj(this.getInstallSaveVo().getAzsj());
        bo.setZbnf(this.getInstallSaveVo().getZbnf());
        bo.setCgly(this.getInstallSaveVo().getCgly());
        bo.setGzzj(this.getInstallSaveVo().getGzzj());
        // 建筑信息
        bo.setSfnc(this.getBuildingSaveVo().getSfnc());
        bo.setHeatingArea(this.getBuildingSaveVo().getHeatingArea());
        bo.setBuildingArea(this.getBuildingSaveVo().getBuildingArea());
        bo.setCycnmj(this.getBuildingSaveVo().getCycnmj());
        bo.setBuildingstructure(this.getBuildingSaveVo().getBuildingStructure());
        bo.setBuildingHeight(this.getBuildingSaveVo().getBuildingHeight());
        bo.setWallthickness(this.getBuildingSaveVo().getOuterWallThickness());
        bo.setKeepwarm(this.getBuildingSaveVo().getOuterWallKeepWarm());
        bo.setGnfs(this.getBuildingSaveVo().getGnfs());
        bo.setInstallposition(this.getBuildingSaveVo().getInstallPosition());
        bo.setFwyt(this.getBuildingSaveVo().getFwyt());
        bo.setFwjs(this.getBuildingSaveVo().getFwjs());
        bo.setSjdw(this.getBuildingSaveVo().getSjdw());
        bo.setTzbh(this.getBuildingSaveVo().getTzbh());
        bo.setChlx(this.getBuildingSaveVo().getChlx());
        bo.setSjrfh(this.getBuildingSaveVo().getSjrfh());
        bo.setFwcx(this.getBuildingSaveVo().getFwcx());
        bo.setMdgzzt(this.getBuildingSaveVo().getMdgzzt());
        bo.setCnmd(this.getBuildingSaveVo().getCnmd());
        // 设备信息
        this.setDeviceSaveBos(toDeviceSaveBos());
        bo.setDeviceSaveBos(this.getDeviceSaveBos());
        // 图纸信息
        bo.setDesignImg(this.getImageSaveVo().getDesignImg());
        bo.setPlaneImg(this.getImageSaveVo().getPlanImg());
        bo.setAztpath1(this.getImageSaveVo().getAztpath1());
        bo.setAztpath2(this.getImageSaveVo().getAztpath2());
        bo.setAztpath3(this.getImageSaveVo().getAztpath3());
        bo.setAztpath4(this.getImageSaveVo().getAztpath4());
        bo.setAztpath5(this.getImageSaveVo().getAztpath5());
        bo.setAztpath6(this.getImageSaveVo().getAztpath6());

        // 操作人信息
        bo.settingOperateInfo(this);
        return bo;
    }

    /**
     * 转换为长效管护设备档案数据模型
     * @param ccCompanyId 长效管护公司
     * @return
     */
    public CallCenterEquipmentArchivesBo toCallCenterEquipmentArchivesBo(String ccCompanyId,
            String opeManufactureId) throws Exception {
        CallCenterEquipmentArchivesBo bo = new CallCenterEquipmentArchivesBo();
        bo.setContact(this.getCustomerSaveVo().getMasterName());
        bo.setContactPhone(this.getCustomerSaveVo().getPhone());
        bo.setIdCardNumber(this.getCustomerSaveVo().getIdCardNo());
        bo.setProjectAddress(this.getCustomerSaveVo().getAddress());
        bo.setAreaCode(this.getCustomerSaveVo().getAreaCode());
        bo.setLongitude(this.getCustomerSaveVo().getJd());
        bo.setLatitude(this.getCustomerSaveVo().getWd());
        bo.setManufactureId(opeManufactureId);
        bo.setCallCenterCompanyId(Long.parseLong(ccCompanyId));
        bo.setDdId(this.getLocationId());
        bo.setDeviceSaveBos(this.getDeviceSaveBos());
        bo.setSource(BusinessType._ctce.getKey());
        bo.setMaintainanceUserId(this.getInstallSaveVo().getAzryId());

        // 操作人信息
        bo.settingOperateInfo(this);
        return bo;
    }

    /**
     * 转换为设备保存业务模型
     * @return
     */
    private List<DeviceSaveBo> toDeviceSaveBos() throws Exception {
        List<DeviceSaveBo> list = new ArrayList<>();
        for (DeviceSaveVo vo : this.deviceSaveVos) {
            DeviceSaveBo bo = new DeviceSaveBo();
            if (StrUtil.isEmpty(vo.getSbId())) {
                // 没有设备ID，表示新增，生成设备ID
//                bo.setSbId(DBUtils.getDjbh(this.getOperateUserCompanyId(), "SB_"));
                bo.setSbId("SB_" + IdUtils.getIncreaseIdByNanoTime());
                bo.setDeleted(DeleteEnum._0.getKey());
                bo.setGmtCreate(new Date());
                bo.setCreateUserName(this.getOperateUserName());
                bo.setCreateUserId(this.getOperateUserId());
            } else {
                // 不为空，表示修改
                bo.setSbId(vo.getSbId());
                if (ObjectUtil.isNotNull(vo.getDeleted())) {
                    bo.setDeleted(vo.getDeleted());
                }
                bo.setGmtModified(new Date());
                bo.setModifiedUserName(this.getOperateUserName());
                bo.setModifiedUserId(this.getOperateUserId());
            }
            bo.setDdId(vo.getDdId());
            bo.setManufacturerId(vo.getManufactureId());
            bo.setDeviceTypeId(vo.getDeviceTypeId());
            bo.setDeviceModelId(vo.getDeviceModelId());
            bo.setDeviceSerialNo(vo.getDeviceSerialNo());
            bo.setDtuId(vo.getDtuId());
            bo.setWarrantyDate(vo.getWarrantyDate());
            bo.setImages(vo.getImages());
            bo.setRemark(vo.getRemark());
            list.add(bo);
        }
        return list;
    }

}